<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\KeywordOne;
use App\Admin\Forms\KeywordTwo;
use App\Admin\Forms\Music;
use App\Admin\Forms\NaturalSpelling;
use App\Admin\Forms\Paint;
use App\Admin\Forms\Tests;
use App\Admin\Forms\Video;
use App\Admin\Grid\Tools\AddUnit;
use App\Admin\Repositories\TeachingMaterial;
use App\Admin\Repositories\MaterialUnit;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Models\Administrator;
use App\Models\TeachingMaterial as Model;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class TeachingMaterialController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::style(
            <<<CSS
        table {
            text-align: center;
        }
CSS
        );
        $user = Admin::user();
        if ($user->is_admin == -1) {
            $book = $user->books()->getQuery();
            $books = new TeachingMaterial($book);
        } else {
            $books = new TeachingMaterial();
        }
        return Grid::make($books, function (Grid $grid) use ($user) {
            if ($user->is_admin == 1) {
                $grid->model()->with(['mechanism']);
            }
            $grid->column('id')->sortable();
            if ($user->is_admin == 1) {
                $grid->column('mechanism.name', '所属机构');
            }
            $grid->column('name')->display(function ($value) {
                $url = 'books/'.$this->getKey().'/unit';
                return "<a href={$url}>".$value."</a>";
            });
            $grid->column('number');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $user = Admin::user();
        if ($user->is_admin == -1) {
            $book = new TeachingMaterial();
        } else {
            $book = new TeachingMaterial('mechanism');
        }
        return Show::make($id, $book, function (Show $show) use ($user) {
            $show->field('id');
            if ($user->is_admin == 1) {
                $show->field('mechanism.name', '所属机构');
            }
            if ($user->is_admin == -1) {
                $show->getKey();
                $book = \App\Models\TeachingMaterial::find($show->getKey());
                if ($user->is_admin == -1 && $book->mechanism_id != $user->id) {
                    abort(404);
                }
            }
            $show->field('name');
            $show->field('number');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $user = Admin::user();
        return Form::make(new TeachingMaterial(), function (Form $form) use ($user) {
            $form->display('id');
            $id = $form->getKey();
            if ($id) {
                $book = \App\Models\TeachingMaterial::find($id);
                if ($user->is_admin == -1 && $book->mechanism_id != $user->id) {
                    abort(404);
                }
            }
            if ($user->is_admin == 1) {
                $form->select('mechanism_id', '所属机构')
                    ->options(Administrator::where('is_admin', -1)
                        ->pluck('name', 'id')->toArray())->required();
            }
            $form->text('name')->required();
            if (!$id) {
                $form->number('number')->min(1)->required()->default(1);
            }
            if ($user->is_admin == -1) {
                $form->hidden('mechanism_id')->default(Admin::user()->id);
            }
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function unitList(Content $content, Model $book)
    {
        if (Admin::user()->is_admin == -1 && $book->mechanism_id != Admin::user()->id) {
            abort(404);
        }
        $books = $book->units()->getQuery();
        $bookId = $book->id;

        $contents = Grid::make(new MaterialUnit($books), function (Grid $grid) use ($bookId) {
            $grid->column('id')->sortable();
            $grid->number('名称')->display(function ($value) {
                return 'unit-' . $value;
            });

            $grid->column('keyword_one_id', '词汇一')
                ->if(function ($column) {
                    return $column->getValue() == 0;
                })
                ->then(function (Grid\Column $column) {
                    $column->display('添加')->modal(KeywordOne::make());
                })
                ->else(function (Grid\Column $column) {
                    $column->display('修改')->modal(KeywordOne::make());
                });
            $grid->column('keyword_two_id', '词汇二')
                ->if(function ($column) {
                    return $column->getValue() == 0;
                })
                ->then(function (Grid\Column $column) {
                    $column->display('添加')->modal(KeywordTwo::make());
                })
                ->else(function (Grid\Column $column) {
                    $column->display('修改')->modal(KeywordTwo::make());
                });
            $grid->column('paint_id', '绘本')
                ->if(function ($column) {
                    return $column->getValue() == 0;
                })
                ->then(function (Grid\Column $column) {
                    $column->display('添加')->modal(Paint::make());
                })
                ->else(function (Grid\Column $column) {
                    $column->display('修改')->modal(Paint::make());
                });
            $grid->column('natural_spelling_id', '自然拼读')
                ->if(function ($column) {
                    return $column->getValue() == 0;
                })
                ->then(function (Grid\Column $column) {
                    $column->display('添加')->modal(NaturalSpelling::make());
                })
                ->else(function (Grid\Column $column) {
                    $column->display('修改')->modal(NaturalSpelling::make());
                });
            $grid->column('video_id', '视频')
                ->if(function ($column) {
                    return $column->getValue() == 0;
                })
                ->then(function (Grid\Column $column) {
                    $column->display('添加')->modal(Video::make());
                })
                ->else(function (Grid\Column $column) {
                    $column->display('修改')->modal(Video::make());
                });
            $grid->column('music_id', '音乐')
                ->if(function ($column) {
                    return $column->getValue() == 0;
                })
                ->then(function (Grid\Column $column) {
                    $column->display('添加')->modal(Music::make());
                })
                ->else(function (Grid\Column $column) {
                    $column->display('修改')->modal(Music::make());
                });
            $grid->column('test_id', '测试')
                ->if(function ($column) {
                    return $column->getValue() == 0;
                })
                ->then(function (Grid\Column $column) {
                    $column->display('添加')->modal(Tests::make());
                })
                ->else(function (Grid\Column $column) {
                    $column->display('修改')->modal(Tests::make());
                });
            $grid->disableCreateButton();
            $addUnit = AddUnit::make()->setKey($bookId);
            $grid->tools($addUnit);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
        return $content
            ->title('教材单元列表')
            ->description('教材单元列表')
            ->body($contents);
    }

    public function select(Request $request)
    {
        $provinceId = $request->get('q');

        $class = Model::find($provinceId);
        $units = $class->units()->orderBy('id', 'asc')->get(['id']);
        $result = [];
        if ($units->count() == 0) {
            return $result;
        }
        foreach ($units as $key => $unit) {
            $result[] = [
                'id' => $unit->id,
                'text' => 'unit-'.($key+1)
            ];
        }
        return $result;
    }
}
